<?php

class InfComponent extends Object {
    var $uses = array('User','Location','Friendship','Checkin');
    
    function inflence_user_location($location,$user,$t1,$t2){
        
        $t = ClassRegistry::init('Checkin');
        $tf = ClassRegistry::init('Friendship');
        $influenced_users=array();
        $time=$t->query("
                select min(time) as t 
                from checkins
                where user_id=".$user."
                    and location_id='".$location."'
                    and time between '".$t1."' 
                        and '".$t2."'");
        $friends=$tf->find('list',
                    array(
                        'fields'=>array('withuser'),
                        'conditions'=>array('user_id'=>$user)
            ));
        $influenced_users=$t->find('list',
                    array(
                        'fields'=>array('user_id','user_id'),
                        'conditions'=>array(
                            'location_id'=>$location,
                            'user_id'=>$friends,'time BETWEEN ? AND ?' => 
                                array($time[0][0]['t'],$t2))));
//the user will influence itself
        $user1=array($user=>$user);
        if(empty($influenced_users))
            return $user1; 
        return array_merge($influenced_users,$user1);
       // return $influenced_users;
    }
    
    /**
     * Tim tap hop nut anh huong boi 1 nut
     * @param local 
     * @param user
     * @param   t1 bat dau thoi diem xet
     * @param   t2 doan cuoi cua diem xet
     * @param   id lay id tu bang tests
     * 
     * @return  thuc thi cac cau lenh SQL do vao results
     * */
    function inflence_user_location_sql($location,$user,$t1,$t2,$id){
        
        $t = ClassRegistry::init('Test');
            
        if(empty($id)){
           
            $idtemp=$t->find('first',
                array(
                    'conditions'=>array(
                        'location_id'=>$location,
                        'user_id'=>$user,
                        't1'=>$t1,
                        't2'=>$t2)));
            $id=$idtemp['Test']['id'];
        }
        $time=$t->query("
                select min(time) as t 
                from checkins
                where user_id=".$user."
                    and location_id='".$location."'
                    and time between '".$t1."' 
                        and '".$t2."'");
        //chinh no se anh huong de no
     //   $run=$t->query('insert into results(test_id,u1,u2) values ('.$id.','.$user.','.$user.')');
        $run=$t->query("
         insert into results(test_id,u1,u2)
            select distinct ".$id.",".$user.",user_id 
            from checkins 
            where   location_id='".$location."' 
                and time BETWEEN '".$time[0][0]['t']."' and '".$t2."'
                and user_id in 
                    (select withuser from friendships where user_id=".$user.")
        ");

    }
}

?>